/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
# -*- coding: utf-8 -*-
import sys, time
sys.path.append("../../../interfaces/wrap/Python/lib")
sys.path.append("../../../../GIMI/wrap/Python/lib")
import TaskCtrlClient
import gimi

g = gimi.GIMI()
tcc = TaskCtrlClient.CTaskCtrlClient(g, 0)

#ret = g.connectToHubEx("asrobo.hut.fi", 40002, sys.argv[1])

host = "localhost"
port = 40002;
print "connecting to  ", host,":",port
ret = g.connectToHubEx(host,port, "")
print TaskCtrlClient.GetErrorStr(ret)

#print "setting default target '", sys.argv[1],"'"
#tcc.SetDefaultTarget(TaskCtrlClient.SMaCISL(sys.argv[1]))
tcc.SetDefaultTarget(TaskCtrlClient.SMaCISL("g.MaCI_TaskCtrl.i"))
#tcc.SetDefaultTarget("")
print "********************************************************************"
print "********************************************************************"
print "Opening TaskCtrlClient:",
ret = tcc.Open()
print TaskCtrlClient.GetErrorStr(ret)
del ret
print "********************************************************************"
print "********************************************************************"

print "Stopping TaskCtrl.."

ret = tcc.StopTaskCtrl();
if ret == True:
    print "OK!"
else:
    print "Failed!"
   
print "********************************************************************"
print "********************************************************************"
time.sleep(1);

print "Starting TaskCtrl.."
ret = tcc.StartTaskCtrl();
if ret == True:
    print "OK!"
else:
    print "Failed!"
   
print "********************************************************************"
print "********************************************************************"
time.sleep(1);

print "Pausing TaskCtrl.."

ret = tcc.PauseTaskCtrl();
if ret == True:
    print "OK!"
else:
    print "Failed!"
   
print "********************************************************************"
print "********************************************************************"
print "Starting again TaskCtrl.."
ret = tcc.StartTaskCtrl();
if ret == True:
    print "OK!"
else:
    print "Failed!"
   
print "********************************************************************"
print "********************************************************************"
time.sleep(1);

id = TaskCtrlClient.TTaskCtrlTaskID(1,2,3,4,5,5,6,7)
print "Adding Task with ID 1,2,3,4,5,6,7"
xml = "<foo bar> jepujee </foo bar>\n<id>1 2 3 4 5</id>"
print xml
timeout = 5000;
ret = tcc.AddTask(id,xml,timeout)
if ret == True:
    
    print "OK!"
    print "Got for id ",id.typeID0,id.typeID1,id.typeID2,id.typeID3,id.typeID4,id.typeID5,id.typeID6, "host:",id.host, "Owner:",id.owner,"Unique TaskID:",id.taskID
    uniqueTaskID = id.taskID
else:
    print "Failed!"
print "********************************************************************"
print "********************************************************************"   
time.sleep(1)
print "Getting task state"
states = TaskCtrlClient.TTaskCtrlTaskState()

if tcc.GetTaskState(uniqueTaskID,states,4000):
    print "Got state"
    print "ID: ",states.taskID.typeID0,states.taskID.typeID1,states.taskID.typeID2,states.taskID.typeID3,states.taskID.typeID4,states.taskID.typeID5,states.taskID.typeID6, "host:",states.taskID.host, "Owner:",states.taskID.owner,"Unique TaskID:",states.taskID.taskID
    print "Percent complete: ",states.percentComplete, ",ETC: ",states.ETC_ms,"ms , Task State: ",states.taskState

else:
    print "FaileD"
print "********************************************************************"
print "********************************************************************"
time.sleep(1)
state_array = TaskCtrlClient.TTaskCtrlTaskStateContainer()
taskxmls = TaskCtrlClient.TStringContainer();


if tcc.GetTaskTable(state_array,taskxmls,5000):
    print "GOT TASK TABLE, Printing elements:"
    if state_array.GetArraySize() == taskxmls.GetArraySize():
        n = state_array.GetArraySize()
        for i in range(n):
            print i,". Task:"
            id = state_array.GetArrayElement(i)
            xml = taskxmls.GetArrayElement(i)
            print "ID: ", id.taskID.typeID0,id.taskID.typeID1,id.taskID.typeID2,id.taskID.typeID3,id.taskID.typeID4,id.taskID.typeID5,id.taskID.typeID6, "host:",id.taskID.host, "Owner:",id.taskID.owner,"Unique TaskID:",id.taskID.taskID
            print "XML:\n"
            print xml
else:
    print "Failed to get Task TABLE"
print "********************************************************************"
print "********************************************************************"
time.sleep(1)

state_array_lib = TaskCtrlClient.TTaskCtrlTaskStateContainer()
taskxmls_lib = TaskCtrlClient.TStringContainer();
if tcc.GetTaskLibrary(state_array_lib,taskxmls_lib,5000):
    print "GOT Task LIBRARY, Printing elements:"
    if state_array_lib.GetArraySize() == taskxmls_lib.GetArraySize():
        n = state_array_lib.GetArraySize()
        for i in range(n):
            print i,". Task:"
            id = state_array_lib.GetArrayElement(i)
            xml = taskxmls_lib.GetArrayElement(i)
            print "ID: ",  id.taskID.typeID0,id.taskID.typeID1,id.taskID.typeID2,id.taskID.typeID3,id.taskID.typeID4,id.taskID.typeID5,id.taskID.typeID6, "host:",id.taskID.host, "Owner:",id.taskID.owner,"Unique TaskID:",id.taskID.taskID
            print "XML:\n"
            print xml
else:
    print "Failed to get Task LIBRARY"
print "********************************************************************"
print "********************************************************************"
time.sleep(1);


id = TaskCtrlClient.TTaskCtrlTaskID(1,2,3,4,5)

taskCtrl_data = TaskCtrlClient.CTaskCtrlData()
sequence = 0
timeout_ms = 1000;
print "Waiting fot taskctrl event for",timeout_ms,"ms"
ret = tcc.WaitForTaskCtrlEvent(taskCtrl_data,sequence,timeout_ms)
if ret[0]:
    ###In python you must set the sequence number with the second return value...
    sequence = ret[1]
    print "GOT TaskCtrlEvent printing.."
    cmd= taskCtrl_data.GetCommand()
    if cmd:
        print "Got command ",cmd.cmd
    else:
        event = taskCtrl_data.GetTaskCtrlStatusEvent()
        if event:
            print "Got taskctrl status event (Sequence n.",sequence,"):"
            print "TaskID:",event.taskID.typeID0,event.taskID.typeID1,event.taskID.typeID2,event.taskID.typeID3,event.taskID.typeID4,event.taskID.typeID5,event.taskID.typeID6, "host:",event.taskID.host, "Owner:",event.taskID.owner,"Unique TaskID:",event.taskID.taskID    
            print "Task state",event.taskState
            print "ETC_ms ", event.ETC_ms
            print "percentComplete", event.percentComplete
        else:
            print "NO STATUS EVENT CONTAINER when got an event???"

else:
    print "No taskctrl event within timeout time"

print "********************************************************************"
print "********************************************************************"
time.sleep(1)
print "Starting task ",uniqueTaskID
ret = tcc.StartTask(uniqueTaskID)
if ret == True:
    print "OK!"
else:
    print "FAILED!"
print "********************************************************************"
print "********************************************************************"
##Sleeps are only to have some time to read the printed lines... You don't need them in your program
time.sleep(1);



print "Stopping task ",uniqueTaskID
ret = tcc.StopTask(uniqueTaskID)
if ret == True:
    print "OK!"
else:
    print "FAILED!"

print "********************************************************************"
print "********************************************************************"
time.sleep(1);
print "Waiting fot taskctrl event for",timeout_ms,"ms"
ret = tcc.WaitForTaskCtrlEvent(taskCtrl_data,sequence,timeout_ms)
if ret[0]:
    ###In python you must set the sequence number with the second return value...
    sequence = ret[1]
    print "GOT TaskCtrlEvent printing.."
    cmd= taskCtrl_data.GetCommand()
    if cmd :
        print "Got command ",cmd.cmd
    else:
        event = taskCtrl_data.GetTaskCtrlStatusEvent()
        if event:
            print "Got taskctrl status event (Sequence n.",sequence,"):"
            print "TaskID:",event.taskID.typeID0,event.taskID.typeID1,event.taskID.typeID2,event.taskID.typeID3,event.taskID.typeID4,event.taskID.typeID5,event.taskID.typeID6, "host:",event.taskID.host, "Owner:",event.taskID.owner,"Unique TaskID:",event.taskID.taskID    

            print "Task state",event.taskState
            print "ETC_ms ", event.ETC_ms
            print "percentComplete", event.percentComplete
        else:
            print "NO STATUS EVENT CONTAINER when got an event???"

else:
    print "No taskctrl event within timeout time"
print "********************************************************************"
print "********************************************************************"
time.sleep(1);

print "Starting again task",uniqueTaskID
ret = tcc.StartTask(uniqueTaskID)
if ret == True:
    print "OK!"
else:
    print "FAILED!"
print "********************************************************************"
print "********************************************************************"
time.sleep(1);


print "Pausing task", uniqueTaskID
ret = tcc.PauseTask(uniqueTaskID)
if ret == True:
    print "OK!"
else:
    print "FAILED!"
print "********************************************************************"
print "********************************************************************"
time.sleep(1);


print "Continuing task ",uniqueTaskID
ret = tcc.ContinueTask(uniqueTaskID)
if ret == True:
    print "OK!"
else:
    print "FAILED!"
print "********************************************************************"
print "********************************************************************"
time.sleep(1);
print "Waiting fot taskctrl event for",timeout_ms,"ms"
ret = tcc.WaitForTaskCtrlEvent(taskCtrl_data,sequence,timeout_ms)
if ret[0]:
    ###In python you must set the sequence number with the second return value...
    sequence = ret[1]
    print "GOT TaskCtrlEvent printing.."
    cmd= taskCtrl_data.GetCommand()
    if cmd :
        print "Got command ",cmd.cmd
    else:
        event = taskCtrl_data.GetTaskCtrlStatusEvent()
        if event:
            print "Got taskctrl status event (Sequence n.",sequence,"):"
            print "TaskID:",event.taskID.typeID0,event.taskID.typeID1,event.taskID.typeID2,event.taskID.typeID3,event.taskID.typeID4,event.taskID.typeID5,event.taskID.typeID6, "host:",event.taskID.host, "Owner:",event.taskID.owner,"Unique TaskID:",event.taskID.taskID 
            
            print "Task state",event.taskState
            print "ETC_ms ", event.ETC_ms
            print "percentComplete", event.percentComplete
        else:
            print "NO STATUS EVENT CONTAINER when got an event???"

else:
    print "No taskctrl event within timeout time"
print "********************************************************************"
print "********************************************************************"
time.sleep(1);
print "Removing task from TABLE"

print "Remove Task with id ",uniqueTaskID
ret = tcc.RemoveTask(uniqueTaskID)
if ret == True:
    print "OK!"
else:
    print "FAILED!"

time.sleep(1);

print "********************************************************************"
print "********************************************************************"
print "Adding a task to LIBRARY"
errorString ="ff"

timeout =1000
ret = tcc.AddTaskToLibrary(id, xml,timeout, errorString)
if ret == True:
    print "OK"
else:
    print "FAILED! ErrorString: ",errorString
                           

time.sleep(1)

print "********************************************************************"
print "********************************************************************"
print "Removing the same task from LIBRARY"

print "Remove Task with ID 1,2,3,4,5"
ret = tcc.RemoveTaskFromLibrary(id)
if ret == True:
    print "OK!"
else:
    print "FAILED!"

time.sleep(1);

print "********************************************************************"
print "********************************************************************"
print "Closing TaskCtrl client..."
ret =tcc.Close()
print TaskCtrlClient.GetErrorStr(ret)
del ret
print "********************************************************************"
print "********************************************************************"
print "All functions called, exiting"
